const joi = require('joi');
const dbQuery = require('../model/db');
const {
	sendError,
	sendSuccess
} = require('../utils/sendFunction');

// 验证规则
const schemaComment = joi.object({
	userid: joi.number().required().error(new Error('userid不符合验证规则')),
	type: joi.number().valid(0, 1, 2).required().error(new Error('type不符合验证规则')),
	content: joi.string().required().error(new Error('content不符合验证规则')),
	create_time: joi.string().required().error(new Error('create_time不符合验证规则'))
})

const schemaComment0 = joi.object({
	musicid: joi.number().required().error(new Error('musicid不符合验证规则'))
})

const schemaComment1 = joi.object({
	gedanid: joi.number().required().error(new Error('gedanid不符合验证规则'))
})

const schemaComment2 = joi.object({
	mvid: joi.number().required().error(new Error('mvid不符合验证规则'))
})

const schemaGetComment = joi.object({
	type: joi.number().valid(0, 1, 2).required().error(new Error('type不符合验证规则'))
})

const schemaDeleteComment = joi.object({
	id: joi.number().required().error(new Error('id不符合验证规则')),
})

// userid type(0歌曲 1歌单 2mv) musicid gedanid mvid content create_time
const addComment = async function (req, res, next) {
	let info = req.body;

	if (!info.userid || !String(info.type) || !info.content || !info.create_time) {
		sendError(res, 200, 'userid或type或评论内容或创建时间不能为空!');
		return;
	}

	try {
		await schemaComment.validateAsync({
			userid: info.userid,
			type: info.type,
			content: info.content,
			create_time: info.create_time
		})
	} catch (error) {
		next(error);
		return;
	}

	// 歌曲
	if (info.type == 0) {
		try {
			await schemaComment0.validateAsync({
				musicid: info.musicid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'insert into comment (user_id,type,music_id,comment_content,comment_create_time) values (?,?,?,?,?)';
		let result = await dbQuery(sqlStr, [info.userid, info.type, info.musicid, info.content, info.create_time]);
		if (result.affectedRows == 1) {
			sendSuccess(res, '添加评论成功!');
		} else {
			sendError(res, 500, '添加评论失败，请稍后再试!')
		}
		// 歌单
	} else if (info.type == 1) {
		try {
			await schemaComment1.validateAsync({
				gedanid: info.gedanid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'insert into comment (user_id,type,gedan_id,comment_content,comment_create_time) values (?,?,?,?,?)';
		let result = await dbQuery(sqlStr, [info.userid, info.type, info.gedanid, info.content, info.create_time]);
		if (result.affectedRows == 1) {
			sendSuccess(res, '添加评论成功!');
		} else {
			sendError(res, 500, '添加评论失败，请稍后再试!')
		}
		// mv
	} else {
		try {
			await schemaComment2.validateAsync({
				mvid: info.mvid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'insert into comment (user_id,type,mv_id,comment_content,comment_create_time) values (?,?,?,?,?)';
		let result = await dbQuery(sqlStr, [info.userid, info.type, info.mvid, info.content, info.create_time]);
		if (result.affectedRows == 1) {
			sendSuccess(res, '添加评论成功!');
		} else {
			sendError(res, 500, '添加评论失败，请稍后再试!')
		}
	}
}

// id
const deleteComment = async function (req, res, next) {
	let {
		id
	} = req.query;

	try {
		await schemaDeleteComment.validateAsync({
			id
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from comment where commentid=?';
	let result = await dbQuery(sqlStr, [id]);
	if (result.length == 0) {
		sendError(res, 200, '删除评论失败，已删除该评论！');
		return;
	}

	let sqlStr1 = 'delete from comment where commentid=?';
	let result1 = await dbQuery(sqlStr1, [id]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '删除评论成功!');
	} else {
		sendError(res, 500, '删除评论失败，请稍后再试!')
	}
}

// type musicid gedanid mvid
const getComment = async function (req, res, next) {
	let info = req.query;

	if (!String(info.type)) {
		sendError(res, 200, 'type不能为空!');
		return;
	}

	try {
		await schemaGetComment.validateAsync({
			type: info.type
		})
	} catch (error) {
		next(error);
		return;
	}

	if (info.type == 0) {
		try {
			await schemaComment0.validateAsync({
				musicid: info.musicid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select comment.*,user.userid,user.username,user.user_nickname,user.user_avator_url from comment inner join user on comment.user_id = user.userid where type=? and music_id=?  order by comment.commentid desc'
		let result = await dbQuery(sqlStr, [info.type, info.musicid]);
		if (result.length >= 0) {
			sendSuccess(res, '获取评论成功!', {
				data: result
			});
		} else {
			sendError(res, 500, '获取评论失败，请稍后再试!')
		}
	} else if (info.type == 1) {
		try {
			await schemaComment1.validateAsync({
				gedanid: info.gedanid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select comment.*,user.userid,user.username,user.user_nickname,user.user_avator_url from comment inner join user on comment.user_id = user.userid where type=? and gedan_id=?  order by comment.commentid desc'
		let result = await dbQuery(sqlStr, [info.type, info.gedanid]);
		if (result.length >= 0) {
			sendSuccess(res, '获取评论成功!', {
				data: result
			});
		} else {
			sendError(res, 500, '获取评论失败，请稍后再试!')
		}
	} else {
		try {
			await schemaComment2.validateAsync({
				mvid: info.mvid
			})
		} catch (error) {
			next(error);
			return;
		}

		let sqlStr = 'select comment.*,user.userid,user.username,user.user_nickname,user.user_avator_url from comment inner join user on comment.user_id = user.userid where type=? and mv_id=? order by comment.commentid desc'
		let result = await dbQuery(sqlStr, [info.type, info.mvid]);
		if (result.length >= 0) {
			sendSuccess(res, '获取评论成功!', {
				data: result
			});
		} else {
			sendError(res, 500, '获取评论失败，请稍后再试!')
		}
	}
}


module.exports = {
	addComment,
	deleteComment,
	getComment
}